package com.example.clxy.controller;

import com.example.clxy.entity.User;
import com.example.clxy.entity.dto.LoginUserDto;
import com.example.clxy.interceptor.AppToken;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.UserPageResult;
import com.example.clxy.result.business.UserAccountResult;
import com.example.clxy.result.business.UserResult;
import com.example.clxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author liuhaoqi
 * @Date 2020/8/3 20:36
 * @Version 1.0
 */
@RestController
@RequestMapping("UserController")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 管理员登录
     *
     * @param account
     * @param password
     * @return
     */
    @RequestMapping("loginWeb")
    @CrossOrigin
    public WebRestResult loginWeb(String account, String password, String usertype) {
        WebRestResult result = userService.loginWeb(account, password, usertype);
        return result;
    }

    /**
     * 小程序端用户登录
     *
     * @param code
     * @return
     */
    @RequestMapping("loginUser")
    @CrossOrigin
    public UserResult loginUser(String code) {
        UserResult result = userService.loginUser(code);
        return result;
    }

    /**
     * 登录绑定前根据学号/工号获取信息
     *
     * @param account
     * @return
     */
    @RequestMapping("getUserInfoByAccount")
    @CrossOrigin
    public UserAccountResult getUserInfoByAccount(String account) {
        UserAccountResult result = userService.getUserInfoByAccount(account);
        return result;
    }

    /**
     * 重置密码
     *
     * @param account
     * @return
     */
    @PostMapping("resetPassword")
    @CrossOrigin
    public WebRestResult resetPassword(String account) {

        WebRestResult result = userService.resetPassword(account);
        return result;
    }

    @PostMapping("updatePassword")
    @CrossOrigin
    public WebRestResult updatePassword(String account, String password, String newpassword) {
        WebRestResult result = userService.updatePassword(account, password, newpassword);
        return result;
    }


    /**
     * 获取用户列表接口
     *
     * @param roleid      角色类型,1 管理员 2 老师 3 学生
     * @param teacherid
     * @param teachername
     * @param grade
     * @param classid
     * @param studentname
     * @param studentid
     * @param page
     * @param pagesize
     * @return
     * @Author weichong
     */
    @CrossOrigin
    @RequestMapping("getUserList")
    public PageResult<UserPageResult> getUserList(String roleid, String teacherid, String teachername, String grade, String classid, String studentname, String studentid, int page, int pagesize) {
        PageResult<UserPageResult> res = userService.getUserList(roleid, teacherid, teachername, grade, classid, studentname, studentid, page, pagesize);
        return res;
    }


    /**
     * 修改用户信息
     *
     * @param user 用户类
     * @return
     * @Author weichong
     */
    @CrossOrigin
    @RequestMapping("updateUser")
    public WebRestResult updateUser(@RequestBody User user) {
        WebRestResult res = userService.updateUser(user);
        return res;
    }


    /**
     * 批量删除用户接口
     *
     * @param useridList 存放用户id的数组
     * @return
     * @Author weichong
     */
    @CrossOrigin
    @RequestMapping("deleteUserByUseridList")
    public WebRestResult deleteUserByUseridList(String[] useridList) {
        WebRestResult res = userService.deleteUserByUseridList(useridList);
        return res;
    }


    /**
     * 新增用户接口
     *
     * @param user 用户类
     * @return
     * @Author weichong
     */
    @CrossOrigin
    @RequestMapping("addUser")
    public WebRestResult addUser(@RequestBody User user) {
        WebRestResult res = userService.addUser(user);
        return res;
    }

    /**
     * 得到已经绑定微信号的用户列表
     *
     * @param page
     * @param pagesize
     * @param roleid
     * @param username
     * @param account
     * @return
     */
    @CrossOrigin
    @GetMapping("getWxBindList")
    public PageResult<UserPageResult> getWxBindList(int page, int pagesize, String roleid, String username, String account) {
        PageResult<UserPageResult> result = userService.getWxBindList(page, pagesize, roleid, username, account);
        return result;
    }

}
